<html lang="en" class="h-100">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Blog 登录</title>
    <!-- Favicon icon -->
    <link rel="shortcut icon" href="images/TG-Thumb.png">
    <link href="./css/style.css" rel="stylesheet">
    <script src="https://cdn.staticfile.org/jquery/1.12.3/jquery.min.js"></script>

</head>
<body class="h-100">
<div class="authincation h-100">
    <div class="container h-100">
        <div class="row justify-content-center h-100 align-items-center">
            <div class="col-md-6">
                <div class="authincation-content">
                    <div class="row no-gutters">
                        <div class="col-xl-12">
                            <div class="auth-form">
                                <h4 class="text-center mb-4">Blog 登录</h4>
                                <form action="/login" method="post" onsubmit="login();return false;">
                                    <div class="form-group">
                                        <label class="mb-1"><strong>用户名</strong></label><br>
                                        <input type="text" class="form-control" name="username" id="username">
                                    </div>
                                    <div class="form-group">
                                        <label class="mb-1"><strong>密码</strong></label><br>
                                        <input type="password" class="form-control" name="password" id="password">
                                    </div>
                                    <div class="form-group">
                                        <label class="mb-1"><strong>验证码（看不清？点击图片刷新）</strong></label><br>
                                        <input type="text" class="form-control" name="captchaText" id="captchaText" style="width:70%; float:left;"/>
                                        <img src="/captcha" id="captchaImage" style="width:110px; height:50px; float:right"/>
                                    </div><br>
                                    <div class="form-row d-flex justify-content-between mt-4 mb-2">
                                        <div class="form-group">
                                            <div class="custom-control custom-checkbox ml-1">
                                                <input type="checkbox" class="custom-control-input" name="rememberMe" id="rememberMe">
                                                <label class="custom-control-label" for="rememberMe">记住密码</label>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label style="float: right">没有账号？<a class="text-primary" href="/register.html">注册</a></label> <!--#36C95F-->
<!--                                            <a href="page-forgot-password.html">Forgot Password?</a>-->
                                        </div>
                                    </div>
                                    <div class="text-center">
                                        <input type="submit" class="btn btn-primary btn-block" value="登录">
                                    </div>
                                </form>
<!--                                <div class="new-account mt-3">-->
<!--                                    <p>没有账号？ <a class="text-primary" href="/register">注册</a></p>-->
<!--                                </div>-->
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>


<script type="text/javascript">
    window.onload = function () {
        var captchaImg = document.getElementById("captchaImage");
        captchaImg.onclick = function () {
            captchaImg.src = "/captcha?" + Math.floor(Math.random() * 100)
        }
    };

    function login() {
        var username = $("#username").val();
        var password = $("#password").val();
        var captchaText = $("#captchaText").val();
        var rememberMe = $("#rememberMe").is(":checked");
        if (username === "" || password === "") {
            alert('用户名或密码不能为空！');
            return;
        }
        $.ajax({
            type: "POST",
            url: "/login",
            data: {
                "username": username,
                "password": password,
                "captchaText": captchaText,
                "remember-me": rememberMe
            },
            success: function (json) {
                if(json.isok){
                    location.href = json.data;
                }else{
                    alert(json.message)
                }
            },
            error: function (e) {
                console.log(e.responseText);
            }
        });
    }
</script>
</body>
</html>
